import { reactive } from "vue";
import { resetPwd } from "@/api/auth/user";
import router from "@/router";
import { ElMessage } from "element-plus";

export const _$ = reactive({
  register: {
    rules: reactive({
      username: [
        { required: true, message: "用户账号不能为空", trigger: "blur" },
        {
          min: 3,
          max: 16,
          message: "用户账号长度必须在3到16位之间",
          trigger: "change",
        },
      ],
      password: [
        { required: true, message: "密码不能为空", trigger: "change" },
      ],
    }),
    requestParam: {
      answer: "",
      password: "",
      question: "",
      username: "",
      confirmPwd: "",
    },
    responseParam: {},
  },
});

export const $ = {
  onSubmit(formRef) {
    if (!formRef) {
      return;
    }
    // TODO 确认密码校验
    resetPwd(_$.register.requestParam)
      .then((_) => {
        ElMessage({
          message: "密码找回成功",
          type: "success",
        });
        router.push("/login");
      })
      .catch((err) => {
        console.error(err);
      });
  },

  clean() {
    _$.register.responseParam = {
      answer: "",
      password: "",
      question: "",
      username: "",
      confirmPwd: "",
    };
  },
};

export const to = {
  toLogin() {
    router.push("/login");
  },
};
